package com.ycsj.goldmedalnewconcept.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.flyco.dialog.listener.OnBtnLeftClickL;
import com.flyco.dialog.listener.OnBtnRightClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.bean.QuanziResponse;
import com.ycsj.goldmedalnewconcept.bean.QuanziResponse.CircleInfo;
import com.ycsj.goldmedalnewconcept.utils.ListViewUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.TimeUtil;
import com.ycsj.goldmedalnewconcept.view.CommentsAdapter;
import com.ycsj.goldmedalnewconcept.view.ImageInfo;
import com.ycsj.goldmedalnewconcept.view.ImagePreviewActivity;
import com.ycsj.goldmedalnewconcept.view.NineGridView;
import com.ycsj.goldmedalnewconcept.view.NineGridViewClickAdapter;
import com.ycsj.goldmedalnewconcept.R;

public class UserQuanziAdapter extends BaseAdapter {

	private Context context;
	private List<CircleInfo> data = new ArrayList<QuanziResponse.CircleInfo>();
	private LayoutInflater mInflater;
	private String content;
	private boolean istime;
	private List<String> timelist = new ArrayList<String>();
	private List<Integer> positionlist = new ArrayList<Integer>();

	public void setData(List<CircleInfo> data) {
		this.data = data;
		notifyDataSetChanged();
	}

	public UserQuanziAdapter(Context context, List<CircleInfo> data) {
		this.context = context;
		this.data = data;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public CircleInfo getItem(int position) {
		return (CircleInfo) data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_userquanzi, parent,
					false);
			convertView.setTag(new ViewHolder(convertView));
		}
		final ViewHolder holder = (ViewHolder) convertView.getTag();
		CircleInfo item = getItem(position);

		holder.content = (TextView) convertView.findViewById(R.id.tv_content);
		holder.createTime = (TextView) convertView
				.findViewById(R.id.tv_createTime);
		holder.tv_mouth = (TextView) convertView.findViewById(R.id.tv_mouth);

		holder.ll_right = (LinearLayout) convertView
				.findViewById(R.id.ll_right);
		holder.ll_big = (LinearLayout) convertView.findViewById(R.id.ll_big);
		holder.iv_img1 = (ImageView) convertView.findViewById(R.id.iv_img1);
		holder.iv_img2 = (ImageView) convertView.findViewById(R.id.iv_img2);
		holder.iv_img3 = (ImageView) convertView.findViewById(R.id.iv_img3);
		holder.iv_img4 = (ImageView) convertView.findViewById(R.id.iv_img4);

		holder.content.setText(data.get(position).content); // 消息内容
		
		istime = false;
		
		for (int i = 1; i < data.size(); i++) {
			for (int j = 0; j < i; j++) {
				
				if (data.get(i).update_date.substring(0, 10).equals(
						data.get(j).update_date.substring(0, 10))) {
					positionlist.add(i);
				}
			}
		}
		for (int i = 1; i < positionlist.size(); i++) {
			for (int j = 0; j < i; j++) {
				if(i!=positionlist.size()&&j!=positionlist.size()){
					
				
				if (positionlist.get(i)==positionlist.get(j)) {
					
					positionlist.remove(i);
				}
				}
			}
		}
		
		for (int i = 0; i < positionlist.size(); i++) {
			if(position==positionlist.get(i)){
				istime = true;
			}
		}
		
		if (!istime) {
			String time = "";
			try {
				time = TimeUtil.getTimetoday(data.get(position).update_date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			// 时间
			if ("今天".equals(time)) {
				holder.createTime.setText(time);
				holder.tv_mouth.setVisibility(holder.tv_mouth.GONE);
			} else {
				if (data.get(position).update_date != null
						&& !"".equals(data.get(position).update_date)) {
					// data.get(position).update_date.substring(5,7);//月
					// data.get(position).update_date.substring(8,10);//日

					holder.tv_mouth.setText(data.get(position).update_date
							.substring(5, 7) + "月");
					holder.createTime.setText(data.get(position).update_date
							.substring(8, 10));
					holder.tv_mouth.setVisibility(holder.tv_mouth.VISIBLE);
				}
				// 2017-10-23 17:40:04
			}
		}else{
			holder.tv_mouth.setText("");
			holder.createTime.setText("");
		}

		final ArrayList<ImageInfo> imageInfo = new ArrayList<ImageInfo>();

		if (data.get(position).s_user_picture1 != null
				&& !"".equals(data.get(position).s_user_picture1)) {
			ImageInfo info = new ImageInfo();
			info.setThumbnailUrl(data.get(position).s_user_picture1);
			info.setBigImageUrl(data.get(position).user_picture1);
			if (!"".equals(data.get(position).picture1_width)
					&& !"".equals(data.get(position).picture1_length)) {
				if (data.get(position).picture1_width != null
						&& data.get(position).picture1_length != null) {
					info.setImageViewWidth(Integer.parseInt(data.get(position).picture1_width));
					info.setImageViewHeight(Integer.parseInt(data.get(position).picture1_length));
				}
			}
			imageInfo.add(info);
		}

		if (data.get(position).s_user_picture2 != null
				&& !"".equals(data.get(position).s_user_picture2)) {
			ImageInfo info = new ImageInfo();
			info.setThumbnailUrl(data.get(position).s_user_picture2);
			info.setBigImageUrl(data.get(position).user_picture2);
			imageInfo.add(info);
		}

		if (data.get(position).s_user_picture3 != null
				&& !"".equals(data.get(position).s_user_picture3)) {
			ImageInfo info = new ImageInfo();
			info.setThumbnailUrl(data.get(position).s_user_picture3);
			info.setBigImageUrl(data.get(position).user_picture3);
			imageInfo.add(info);
		}
		if (data.get(position).s_user_picture4 != null
				&& !"".equals(data.get(position).s_user_picture4)) {
			ImageInfo info = new ImageInfo();
			info.setThumbnailUrl(data.get(position).s_user_picture4);
			info.setBigImageUrl(data.get(position).user_picture4);
			imageInfo.add(info);
		}
		if (data.get(position).s_user_picture5 != null
				&& !"".equals(data.get(position).s_user_picture5)) {
			ImageInfo info = new ImageInfo();
			info.setThumbnailUrl(data.get(position).s_user_picture5);
			info.setBigImageUrl(data.get(position).user_picture5);
			imageInfo.add(info);
		}
		if (data.get(position).s_user_picture6 != null
				&& !"".equals(data.get(position).s_user_picture6)) {
			ImageInfo info = new ImageInfo();
			info.setThumbnailUrl(data.get(position).s_user_picture6);
			info.setBigImageUrl(data.get(position).user_picture6);
			imageInfo.add(info);
		}
		if (data.get(position).s_user_picture7 != null
				&& !"".equals(data.get(position).s_user_picture7)) {
			ImageInfo info = new ImageInfo();
			info.setThumbnailUrl(data.get(position).s_user_picture7);
			info.setBigImageUrl(data.get(position).user_picture7);
			imageInfo.add(info);
		}
		if (data.get(position).s_user_picture8 != null
				&& !"".equals(data.get(position).s_user_picture8)) {
			ImageInfo info = new ImageInfo();
			info.setThumbnailUrl(data.get(position).s_user_picture8);
			info.setBigImageUrl(data.get(position).user_picture8);
			imageInfo.add(info);
		}
		if (data.get(position).s_user_picture9 != null
				&& !"".equals(data.get(position).s_user_picture9)) {
			ImageInfo info = new ImageInfo();
			info.setThumbnailUrl(data.get(position).s_user_picture9);
			info.setBigImageUrl(data.get(position).user_picture9);
			imageInfo.add(info);
		}

		// holder.nineGrid.setAdapter(new NineGridViewClickAdapter(context,
		// imageInfo));
		holder.ll_big.setVisibility(holder.ll_big.GONE);
		holder.ll_right.setVisibility(holder.ll_right.GONE);
		holder.iv_img2.setVisibility(holder.iv_img2.GONE);
		holder.iv_img3.setVisibility(holder.iv_img3.GONE);
		holder.iv_img4.setVisibility(holder.iv_img4.GONE);

		if (imageInfo == null || imageInfo.size() == 0) {
			holder.ll_big.setVisibility(holder.ll_big.GONE);
		}
		if (imageInfo.size() == 1) {
			holder.ll_big.setVisibility(holder.ll_big.VISIBLE);

			Picasso.with(context).load(imageInfo.get(0).thumbnailUrl)//
					.into(holder.iv_img1);
		}
		if (imageInfo.size() == 2) {
			holder.ll_big.setVisibility(holder.ll_big.VISIBLE);
			holder.ll_right.setVisibility(holder.ll_right.VISIBLE);
			holder.iv_img2.setVisibility(holder.iv_img2.VISIBLE);

			Picasso.with(context).load(imageInfo.get(0).thumbnailUrl)
					.into(holder.iv_img1);
			Picasso.with(context).load(imageInfo.get(1).thumbnailUrl)
					.into(holder.iv_img2);
		}
		if (imageInfo.size() == 3) {
			holder.ll_big.setVisibility(holder.ll_big.VISIBLE);
			holder.ll_right.setVisibility(holder.ll_right.VISIBLE);
			holder.iv_img2.setVisibility(holder.iv_img2.VISIBLE);
			holder.iv_img3.setVisibility(holder.iv_img3.VISIBLE);

			Picasso.with(context).load(imageInfo.get(0).thumbnailUrl)
					.into(holder.iv_img1);
			Picasso.with(context).load(imageInfo.get(1).thumbnailUrl)
					.into(holder.iv_img2);
			Picasso.with(context).load(imageInfo.get(2).thumbnailUrl)
					.into(holder.iv_img3);
		}
		if (imageInfo.size() > 4 || imageInfo.size() == 4) {
			holder.ll_big.setVisibility(holder.ll_big.VISIBLE);
			holder.ll_right.setVisibility(holder.ll_right.VISIBLE);
			holder.iv_img2.setVisibility(holder.iv_img2.VISIBLE);
			holder.iv_img3.setVisibility(holder.iv_img3.VISIBLE);
			holder.iv_img4.setVisibility(holder.iv_img4.VISIBLE);

			Picasso.with(context).load(imageInfo.get(0).thumbnailUrl)
					.into(holder.iv_img1);
			Picasso.with(context).load(imageInfo.get(1).thumbnailUrl)
					.into(holder.iv_img2);
			Picasso.with(context).load(imageInfo.get(2).thumbnailUrl)
					.into(holder.iv_img3);
			Picasso.with(context).load(imageInfo.get(3).thumbnailUrl)
					.into(holder.iv_img4);
		}
		holder.ll_big.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, ImagePreviewActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable(ImagePreviewActivity.IMAGE_INFO,
						(Serializable) imageInfo);
				bundle.putInt(ImagePreviewActivity.CURRENT_ITEM, 0);
				intent.putExtras(bundle);
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	class ViewHolder {
		private TextView content;
		private ImageView iv_img1;
		private ImageView iv_img2;
		private ImageView iv_img3;
		private ImageView iv_img4;
		private LinearLayout ll_right;
		private LinearLayout ll_big;

		private TextView createTime;
		private TextView tv_mouth;
		private View rootView;

		public ViewHolder(View convertView) {
			rootView = convertView;
			NineGridView.setImageLoader(new PicassoImageLoader());
		}

		/** Picasso 加载 */
		private class PicassoImageLoader implements NineGridView.ImageLoader {

			@Override
			public void onDisplayImage(Context context, ImageView imageView,
					String url) {
				Picasso.with(context).load(url)//
						.placeholder(R.drawable.temp)//
						.error(R.drawable.temp)//
						.into(imageView);
			}

			@Override
			public Bitmap getCacheImage(String url) {

				return null;
			}
		}
	}
}
